using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using WCSFContrib.ECommerceCatalog.BusinessEntities;
using WCSFContrib.ECommerceCatalog.CatalogData;
using WCSFContrib.ECommerceCatalog.Product.Services;
using WCSFContrib.ECommerceCatalog.Product.Tests.Fakes;

namespace WCSFContrib.ECommerceCatalog.Product.Tests.Services
{
    /// <summary>
    /// Summary description for ItemsServiceFixture.
    /// </summary>
    [TestClass]
    public class ItemsServiceFixture
    {
        private CatalogDataSet ds;
        private ItemsService itemsService;

        [TestInitialize()]
        public void MyTestInitialize()
        {
            ds = new CatalogDataSet();

            ds.Category.AddCategoryRow("C1", "General1", 0);
            ds.Category.AddCategoryRow("C2", "General2", 0);
            ds.Category.AddCategoryRow("C1-A", "1-SubGeneral1", 1);
            ds.Category.AddCategoryRow("C1-B", "1-SubGeneral2", 1);
            ds.Category.AddCategoryRow("C2-A", "General3", 2);

            ds.Item.AddItemRow("Item1 Description A", "item1.png", "Item1", 11, 21, ds.Category[0]);
            ds.Item.AddItemRow("Item2 Description A", "item2.png", "Item2", 12, 22, ds.Category[1]);
            ds.Item.AddItemRow("Item3 Description", "item3.png", "Item3", 13, 23, ds.Category[2]);
            ds.Item.AddItemRow("Item4 Description", "item4.png", "Item4", 14, 24, ds.Category[0]);
            ds.Item.AddItemRow("Item5 Description", "item5.png", "Item5", 15, 25, ds.Category[1]);

            ds.AcceptChanges();

            itemsService = new ItemsService(ds, new FakeCategoriesService());
        }

        [TestMethod]
        public void ShouldGetItemById()
        {
            Item item = itemsService.GetItemById(3);
            Assert.AreEqual<int>(3, item.ItemId);
            Assert.AreEqual<string>("Item3", item.Name);
        }

        [TestMethod]
        public void ShouldGetAllItems()
        {
            ICollection<Item> allItems = itemsService.AllItems;
            Assert.IsNotNull(allItems);
            Assert.AreEqual<int>(5, allItems.Count);
        }

        [TestMethod]
        public void ShouldSearchItems()
        {
            ICollection<Item> items = itemsService.SearchItems("Description A");
            Assert.AreEqual<int>(2, items.Count);

            items = itemsService.SearchItems("item", "1");
            Assert.AreEqual<int>(2, items.Count);
        }

        [TestMethod]
        public void ShouldSearchItemsByCategory()
        {
            ICollection<Item> items = itemsService.SearchItems("Description A");
            Assert.AreEqual<int>(2, items.Count);

            items = itemsService.SearchItems("item", "1");
            Assert.AreEqual<int>(2, items.Count);
        }

        [TestMethod]
        public void ShouldGetItemsByCategory()
        {
            ICollection<Item> items = itemsService.GetItemsByCategory(1);

            Assert.IsNotNull(items);
            Assert.AreEqual<int>(2, items.Count);
        }

        [TestMethod]
        public void ShouldUpdateItem()
        {
            Item item = itemsService.GetItemById(4);

            item.Name = "Item4 UPDATED";
            item.Stock = 999;
            itemsService.UpdateItem(item);

            item = itemsService.GetItemById(4);

            Assert.AreEqual<int>(4, item.ItemId);
            Assert.AreEqual<string>("Item4 UPDATED", item.Name);
            Assert.AreEqual<int>(999, item.Stock);
        }
    }
}
